﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO;
using System.Diagnostics;

namespace TrailMap
{
    public class GoogleTileSource:Microsoft.Maps.MapControl.TileSource,IMapProvider
    {
        #region Constants

        //http://khm1.google.co.in/kh/v=76&x=739&y=473&z=10&s=Ga
        public const string sateliteURL = "http://khm{0}.google.co.in//kh/v=76&x={1}&y={2}&z={3}&s=Ga";
        //http://mt1.google.com/vt/lyrs=m@142&hl=en&gl=in&x=5913&y=3785&z=13&s=Gali
        public const string normalURL = "http://mt{0}.google.com/vt/lyrs=m@142&hl=en&gl=in&x={1}&y={2}&z={3}&s=Gali";
        //http://mt1.google.com/vt/lyrs=h@142&hl=en&gl=in&x=1479&y=947&z=11&s=
        public const string hybridURL = "http://mt{0}.google.com/vt/lyrs=h@142&hl=en&gl=in&x={1}&y={2}&z={3}&s=";
        //http://mt1.google.com/vt/lyrs=t@126,r@142&hl=en&gl=in&x=737&y=475&z=10&s=Galile
        public const string terrainURL = "http://mt{0}.google.com/vt/lyrs=t@126,r@142&hl=en&gl=in&x={1}&y={2}&z={3}&s=Galile";

        #endregion

        public GoogleTileSource()
            : base("http://mt2.google.com/vt/lyrs=m@107&hl=lt&x=18&y=10&z=5&s=")
        {
            
        }

        public Microsoft.Maps.MapControl.TileSource HybridBase
        { 
            get
            {
                GoogleTileSource retVal = new GoogleTileSource();
                retVal.MapMode = MapType.Satellite;
                return retVal;
            }
        }

        public string FileExtn
        {
            get { return "jpeg"; }
        }
        public MapType MapMode { get;set;}

        public override Uri GetUri(int x, int y, int zoomLevel)
        {
            try
            {
                string urlTemplate ="";

                switch (this.MapMode)
                {
                    case MapType.Normal:
                        //NormalMap
                        urlTemplate = normalURL;
                        break;
                    case MapType.Hybrid:
                        urlTemplate = hybridURL;
                        break;
                    case MapType.Terrain:
                        urlTemplate = terrainURL;
                        break;
                    default:
                        urlTemplate = sateliteURL;
                        break;
                }

                string url = string.Format(urlTemplate, GetServerNum(x, y, 4), x, y, zoomLevel);
                return new Uri(url);
            }
            catch (Exception ex)
            {
            }
            return null;
        }

        #region Other Helper Methods
        /// <summary>
        /// gets server num based on position
        /// </summary>
        /// <param name="pos"></param>
        /// <returns></returns>
        internal int GetServerNum(int x, int y, int max)
        {
            return (x + 2 * y) % max;
        }
        #endregion
    } 
}
